package controller;

import pojo.Book;
import pojo.User;
import service.BookService;
import service.UserService;
import view.BookClassification;
import view.ModifyBook;
import view.SelfBookDetails;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class BookClickListener implements MouseListener {
    private JTable table;
    private String userId;
    private String userName;
    private BookClassification bookClassification;
    public BookClickListener(String userId, String userName, BookClassification bookClassification) {
        this.userId = userId;
        this.userName = userName;
        this.bookClassification=bookClassification;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        UserService userService=new UserService();
        User user=userService.search(userId);
        if (user.getType().equals("图书管理员")) {
            int selRow = table.getSelectedRow();
            String bookId = table.getValueAt(selRow,0).toString().trim();
            BookService bookService = new BookService();
            Book book =  bookService.searchId(bookId);
            ModifyBook modifyBook = new ModifyBook(book,this.bookClassification);}
        else {
                int selRow = table.getSelectedRow();
                String bookId = table.getValueAt(selRow,0).toString().trim();
                BookService bookService = new BookService();
                Book book =  bookService.searchId(bookId);
                SelfBookDetails selfBookDetails = new SelfBookDetails(book,userId,userName);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    public void setTable(JTable table) {
        this.table=table;
    }


}
